package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.LogicException;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements IRedisService {
    @Autowired
    private StringRedisTemplate template;
    @Override
    public void sendVerifyCode(String phone, String code) {
        //在redis中存入数据,并设置有效生命时间
        String key = RedisKeys.VERIFY_CODE.join(phone);
        template.opsForValue().set(key,code, RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        AssertUtil.hasLength(phone,"手机号码为空");
        String key = RedisKeys.VERIFY_CODE.join(phone);
        String code = template.opsForValue().get(key);
        return code;
    }

    @Override
    public void setToken(String token, String user) {
        if(token==null){
            throw new RuntimeException("令牌为空");
        }
        if(user==null){
            throw new RuntimeException("用户不存在");
        }
        //key
        String key = RedisKeys.LOGIN_TOKEN.join(token);
        template.opsForValue().set(key,user,RedisKeys.LOGIN_TOKEN.getTime(),TimeUnit.SECONDS);
    }

    @Override
    public UserInfo checkVal(String token) {
        if(!StringUtils.hasLength(token)){
            throw new LogicException("用户已失效");
        }
        String key = RedisKeys.LOGIN_TOKEN.join(token);
        String value = template.opsForValue().get(key);
        UserInfo user = JSON.parseObject(value, UserInfo.class);
        /*if(user == null){
            throw new RuntimeException("用户已失效");
        }*/
        //重置时间
        template.expire(key,RedisKeys.LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
       // template.opsForValue().set(key,value,RedisKeys.LOGIN_TOKEN.getTime(),TimeUnit.SECONDS);
        return user;
    }

    //设置发送短信的次数
    @Override
    public void setTime(String phone) {
        String key = RedisKeys.SET_TIME.join(phone);
        template.opsForValue().set(key,"5",RedisKeys.SET_TIME.getTime(),TimeUnit.SECONDS);
    }

    //返回当前短信次数
    @Override
    public Integer getTime(String phone) {
        String key = RedisKeys.SET_TIME.join(phone);
        String value = template.opsForValue().get(key);
        Integer time = Integer.valueOf(value);
        return time;
    }
    //减少有效期内发送短信的次数
    @Override
    public void decrTime(String phone) {
        String key = RedisKeys.SET_TIME.join(phone);
        template.opsForValue().decrement(key,1);
    }
    //判断是否有某个键
    @Override
    public Boolean hasKey(String phone) {
        String key = RedisKeys.SET_TIME.join(phone);
        Boolean hasKey = template.hasKey(key);
        return hasKey;
    }


}
